package com.jinzhending.controller.wx;

import com.jinzhending.comm.StaticUrl;
import com.jinzhending.comm.UploadFile;
import com.jinzhending.entity.GoodsInfo;
import com.jinzhending.service.wx.GoodsListService;
import com.jinzhending.utils.AESUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: LieIdle
 * @Package: com.jinzhending.controller.wx  商品列表
 * @Author: huat
 * @Date: 2020/4/6 8:47
 * @Version: 1.0
 */
@RestController
@RequestMapping("wx/goodsList")
public class GoodsListController {
    @Autowired
    GoodsListService goodsListService;

    /**
     * 通过商品id删除商品
     * @param id
     * @return
     */
    @RequestMapping("delGoodInfoById")
    public Map<String,Object> delGoodInfoById(Integer id){
        Map<String,Object> map=new HashMap<String,Object>();
        if(null==id){
            map.put("code",1);
            map.put("msg","参数不合法");
            map.put("data","");
            return map;
        }
        int result=goodsListService.delGoodInfoById(id);
        map.put("code",result>0?0:1);
        map.put("message",result>0?"删除成功":"删除失败");
        map.put("data","");
        return map;

    }

    /**
     * 获取商品通过id
     * @param id
     * @return
     */
    @RequestMapping("getGoodById")
    public Map<String,Object> getGoodById(Integer id){
        Map<String,Object> map=new HashMap<String,Object>();
        if(null==id){
            map.put("code",1);
            map.put("msg","参数不合法");
            map.put("data","");
            return map;
        }
        GoodsInfo result=goodsListService.getGoodById(id);
        map.put("code",result!=null?0:1);
        map.put("message",result!=null?"获取成功":"获取失败");
        map.put("data", result);
        return map;
    }

    /**
     * 富文本编辑器上传图片
     * @param file
     * @return
     */
    @RequestMapping("upload")
    public Map<String,Object> upload(MultipartHttpServletRequest file){
        Map<String,Object> map=new HashMap<String,Object>();
        String url=StaticUrl.getUrl();
        String str=UploadFile.getFileURL(file,"file",url,"");
        map.put("code",0);
        map.put("message","");
        map.put("data", str);
        return map;
    }

    /**
     * 修改商品
     * @param goodsInfo
     * @return
     */
    @RequestMapping("updateGoodInfo")
    public Map<String,Object> updateGoodInfo(GoodsInfo goodsInfo){
        System.out.println(goodsInfo.getGoodsName());
        return null;
    }
}
